﻿using Airborne.ScrumPoker.Presentation.AutomationTests.Artifacts;
using Core;
using Core.UIItems;
using Core.UIItems.WindowItems;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Airborne.ScrumPoker.Presentation.AutomationTests.Master
{
    [TestClass]
    public class TeamFoundationTestFixture : TestBase
    {
        private const string TeamFoundationServer = "https://tfs.airborneconsulting.co.za:8081/";
        private const string TeamFoundationProjectName = "Airborne_Internal";
    
        protected static Application MasterApplication { get; set; }
        protected static Window ProjectOwnerView { get; set; }


        [ClassInitialize]
        public static void ClassInitialise(TestContext context)
        {
            MasterApplication = GetApplication(TestConstants.MasterApplication);
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            Close(MasterApplication);
        }

        [TestInitialize]
        public void TestInit()
        {
            ProjectOwnerView = OpenWindow(MasterApplication, "shellProjectOwnerView");
        }

        [TestMethod]
        public void EnterBogusTfsServerTest()
        {
            ScrumPokerProjectOwner owner = new ScrumPokerProjectOwner(ProjectOwnerView);
            ConnectToServer(owner, "bad address");
            Assert.IsTrue(ProjectOwnerView.ModalWindows().Count > 0);
            ProjectOwnerView.ModalWindows()[0].Get<Button>().Click();
        }

        [TestMethod, Priority(0)]
        public void SelectTfsServerTest()
        {
            ScrumPokerProjectOwner owner = new ScrumPokerProjectOwner(ProjectOwnerView);
            ConnectToServer(owner, TeamFoundationServer);
            Assert.IsTrue(owner.TeamProjectsComboBox.Items.Count > 0);
        }

        [TestMethod]
        public void SelectProjectTest()
        {
            ScrumPokerProjectOwner owner = new ScrumPokerProjectOwner(ProjectOwnerView);
            SelectProject(owner, TeamFoundationProjectName);
            Assert.IsTrue(owner.WorkItemsListView.Rows.Count > 0);
        }

        [TestMethod]
        public void SelectWorkItemTest()
        {
            ScrumPokerProjectOwner owner = new ScrumPokerProjectOwner(ProjectOwnerView);

            //Assert.IsTrue( string.IsNullOrEmpty(owner.WorkItemTitleTextBlock.Text));
            SelectWorkItem(owner, 1);
            Assert.IsFalse(string.IsNullOrEmpty(owner.WorkItemTitleTextBlock.Text));
        }


        [TestMethod]
        public void SelectNextAndPrerviousWorkItemTest()
        {
            ScrumPokerProjectOwner owner = new ScrumPokerProjectOwner(ProjectOwnerView);
            SelectWorkItem(owner, 1);
            string workItemTitle = owner.WorkItemTitleTextBlock.Text;

            owner.SelectNextWorkItemButton.Click();
            Assert.AreNotEqual(workItemTitle, owner.WorkItemTitleTextBlock.Text);
            owner.SelectPreviousWorkItemButton.Click();
            Assert.AreEqual(workItemTitle, owner.WorkItemTitleTextBlock.Text);

        }

        

        private void ConnectToServer(ScrumPokerProjectOwner owner, string serverName)
        {
            if (owner.TeamFoundationServerComboBox.SelectedItemText != serverName)
            {
                owner.TeamFoundationServerComboBox.Enter(serverName);
            }
            owner.ConnectToTeamFoundationButton.Click();
            ProjectOwnerView.WaitWhileBusy();
        }

        private void SelectProject(ScrumPokerProjectOwner owner, string projectName)
        {
            if (owner.TeamProjectsComboBox.Items.Count <= 0)
            {
                ConnectToServer(owner, TeamFoundationServer);
            }
            if (owner.TeamProjectsComboBox.SelectedItemText != projectName)
            {

                if (owner.TeamProjectsComboBox.Items.Count > 0)
                {
                    owner.TeamProjectsComboBox.Enter(projectName);
                }
            }
        }

        private void SelectWorkItem(ScrumPokerProjectOwner owner, int index)
        {
            if (owner.WorkItemsListView.Rows.Count <= 0)
            {
                SelectProject(owner, TeamFoundationProjectName);
            }

            if (owner.WorkItemsListView.Rows.Count >= index)
            {
                owner.WorkItemsListView.Rows[0].Select();
            }

        }
    }
}
